package com.hn658.groupshare.backend.action.group.impl;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.groupshare.backend.action.group.IGroupInfoAction;
import com.hn658.groupshare.backend.action.group.vo.GroupShareRequest;
import com.hn658.groupshare.backend.action.group.vo.GroupShareResponse;
import com.hn658.groupshare.group.entity.GroupInfoEO;
import com.hn658.groupshare.group.service.IGroupInfoService;

@Controller("groupAction")
@Path("group")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class GroupInfoAction extends AbstractBaseService implements IGroupInfoAction {
	@Autowired
	private IGroupInfoService groupInfoService;

	@Path("queryGroupInfoList")
	@Produces("application/json; charset=UTF-8")
	@POST
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	public IServiceResponse queryGroupInfoList(@FormParam(value = "groupNameLike") String groupNameLike,
			@FormParam(value = "groupType") String groupType, @FormParam(value = "limit") int limit,
			@FormParam(value = "start") int start) {
		GroupShareResponse response = new GroupShareResponse();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			if (!StringUtils.isEmpty(groupNameLike)) {
				params.put("groupNameLike", groupNameLike);
			}
			if (!StringUtils.isEmpty(groupType)) {
				params.put("groupType", Integer.valueOf(groupType));
			}

			params.put("isDeleted", false);
			GenericPage<GroupInfoEO> gp = groupInfoService.queryGroupsByParams(params, limit, start, "create_time",
					false);
			response.setGroupInfoList(gp.getData());
			response.setTotalCount(gp.getTotalCount());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("updateGroup")
	@POST
	public IServiceResponse updateGroup(GroupShareRequest request) {

		GroupShareResponse response = new GroupShareResponse();

		try {

			groupInfoService.updateGroup(request.getGroupInfo());
			this.success(response);
			
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("deleteGroup")
	@POST
	public IServiceResponse deleteGroup(GroupShareRequest request) {

		GroupShareResponse response = new GroupShareResponse();

		try {
			
			GroupInfoEO group = request.getGroupInfo();
			group.setIsDeleted(true);
			groupInfoService.updateGroup(group);

			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
